package com.tust.app.service.impl;




import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.dao.TopicMapper;
import com.tust.app.domain.Answer;
import com.tust.app.domain.News;
import com.tust.app.domain.Topic;
import com.tust.app.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    private TopicMapper topicRepository;

    @Override
    public PageInfo getAllTopics(int pageNum) {
        PageHelper.startPage(pageNum, 3);
        //查询数据库,得到新闻表中的数据列表,丢给页面
        List<Topic> topics = topicRepository.findAllTopics();
        PageInfo page = new PageInfo(topics);
        return page;
    }

    @Override
    public Topic selectTopicAnswerById(int id) {
        return topicRepository.findById(id);
    }

    @Override
    public List<Answer> getAnswerList(int topicId) {
        List<Answer> list=topicRepository.getAnswerList(topicId);
        return list;
    }

    @Override
    public void addTopic(String edituser, String title, String detail) {
        String pubtime= String.valueOf(LocalDate.now());
        topicRepository.addTopic(edituser,title,detail,pubtime);
    }

    @Override
    public PageInfo getAllTopicsByUser(String edituser,int pageNum) {
        PageHelper.startPage(pageNum, 3);
        List<Topic> topics=topicRepository.getAllTopicsByUser(edituser);
        PageInfo page = new PageInfo(topics);

        return page;
    }

    @Override
    public void updateTopic(int id, String title, String detail) {
        topicRepository.updateTopic(id,title,detail);
    }

    @Override
    public Topic getTopicById(int id) {
        Topic topic=topicRepository.getTopicById(id);
        return topic;
    }

    @Override

    public void updateTopicState(int id) {
        Topic topic = topicRepository.findById(id);
        if (topic != null) {
            int state=topic.getState()==0?1:0;
            topicRepository.update(state,id);
        }
    }

    @Override

    public void deleteTopic(int id) {
        topicRepository.deleteById(id);
    }
}
